package com.renwei.yunlong.fragment;

import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseRefreshFragment;
import com.renwei.yunlong.bean.WorkSactionBean;
import com.renwei.yunlong.event.ChartPasgeRefreseEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.RatingBar;
import com.renwei.yunlong.view.RoundedRectProgressBar;

import java.util.HashMap;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by wangqi on 2017/9/29.
 * 满意率
 */

public class WorkSatisfiedFragment extends BaseRefreshFragment implements HttpTaskListener, RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.rb_all)
    RadioButton rbAll;
    @Bind(R.id.rb_mouth)
    RadioButton rbMouth;
    @Bind(R.id.rb_week)
    RadioButton rbWeek;
    @Bind(R.id.rb_day)
    RadioButton rbDay;
    @Bind(R.id.rg)
    RadioGroup rg;
    @Bind(R.id.rpb_5)
    RoundedRectProgressBar rpb5;
    @Bind(R.id.rpb_4)
    RoundedRectProgressBar rpb4;
    @Bind(R.id.rpb_3)
    RoundedRectProgressBar rpb3;
    @Bind(R.id.rpb_2)
    RoundedRectProgressBar rpb2;
    @Bind(R.id.rpb_1)
    RoundedRectProgressBar rpb1;
    @Bind(R.id.ll_1)
    LinearLayout ll1;
    @Bind(R.id.ll_2)
    LinearLayout ll2;
    @Bind(R.id.ll_3)
    LinearLayout ll3;
    @Bind(R.id.ll_4)
    LinearLayout ll4;
    @Bind(R.id.ll_5)
    LinearLayout ll5;
    @Bind(R.id.ll_jindutiao)
    LinearLayout llJindutiao;
    @Bind(R.id.tv_work_all)
    TextView tvWorkAll;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.rtb_star_fives)
    RatingBar rtbStarFives;
    @Bind(R.id.tv_count)
    TextView tvCount;

    private String companyCode; // 筛选公司code
    private String contractId; // 项目id
    private String detailId;
    private String endDates;
    private String startDates;
    private int type;

    private void initProgress() {
        final ViewTreeObserver vto = llJindutiao.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                vto.removeOnPreDrawListener(this);
                return true;
            }
        });
    }


    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_work_satis;
    }

    @Override
    protected void initView() {
        rg.setOnCheckedChangeListener(this);
//        initProgress();
    }

    @Override
    public void onPageCheck() {
        if (type==0){
            rbAll.toggle();
        }else{
            initData();
        }
    }

    /**
     * 筛选
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onScreen(ChartPasgeRefreseEvent event) {
        companyCode = event.getTargetCompanyCode();
        contractId = event.getTargetProjecyCode();
        detailId = event.getTargetServiceCode();

        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            map.put("serviceProviderCode", companyCode);
        } else if ("2".equals(companyType)) {
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            map.put("ownerCode", companyCode);
            map.put("contractId", contractId);
        }
        map.put("dateFlag",type+"");
        map.put("startDates", startDates);
        map.put("endDates", endDates);
        map.put("detailId", detailId);

        ServiceRequestManager.getManager().getWorkSatisfied(getContext(), JsonMapUtil.mapToJson(map),this);
    }

    private void setData(WorkSactionBean bean) {
        WorkSactionBean.Rows.Level1 level1 = bean.getRows().getLevel1();
        WorkSactionBean.Rows.Level2 level2 = bean.getRows().getLevel2();
        WorkSactionBean.Rows.Level3 level3 = bean.getRows().getLevel3();
        WorkSactionBean.Rows.Level4 level4 = bean.getRows().getLevel4();
        WorkSactionBean.Rows.Level5 level5 = bean.getRows().getLevel5();

        rtbStarFives.setStar(level1.getServerGradeR());
        tvCount.setText(level1.getServerGradeR()+"");
        tvWorkAll.setText("已解决工单：" + level1.getTotalNum());
        tvTime.setText(level1.getTimeSlot());


        rpb1.showProgress(level1.getEvalNum(), level1.getTotalNum());
        rpb2.showProgress(level2.getEvalNum(), level2.getTotalNum());
        rpb3.showProgress(level3.getEvalNum(), level3.getTotalNum());
        rpb4.showProgress(level4.getEvalNum(), level4.getTotalNum());
        rpb5.showProgress(level5.getEvalNum(), level5.getTotalNum());

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        switch (id) {
            case R.id.rb_all:
                type= 1;
                break;
            case R.id.rb_mouth:
                type= 2;
                break;
            case R.id.rb_week:
                type= 3;
                break;
            case R.id.rb_day:
                type= 4;
                break;
        }
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                WorkSactionBean value = new Gson().fromJson(result, WorkSactionBean.class);
                int code = value.getMessage().getCode();
                if (code == 200) {
                    setData(value);
                } else {
                    showCenterInfoMsg(value.getMessage().getMessage());
                }
                break;
                default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

}
